package com.mogujie.tt.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.mogujie.tt.R;
import com.mogujie.tt.config.ForgotPasswordConstant;
import com.mogujie.tt.customview.control.PhoneVerificationCode;
import com.mogujie.tt.imservice.event.ForgotPasswordEvent;
import com.mogujie.tt.imservice.manager.IMForgotPasswordManager;
import com.mogujie.tt.imservice.manager.IMRegisterManager;
import com.mogujie.tt.ui.base.TTBaseActivity;

import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by user on 2015/9/18.
 */
public class ForgotPasswordActivity extends TTBaseActivity {

    private LinearLayout mLoginStatusView;//进度条
    private EditText mEditTextForgotPasswordName;
    private EditText mEditTextForgotPasswordCode;//忘记密码的验证码
    private PhoneVerificationCode mPhoneVerificationCode;

    private EditText mEditTextNewPassword;
    private EditText mEditTextConfirmPassword;
    private Button   mNewPasswordButton;

    protected ViewGroup curView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);//注册EventBus
     //   setContentView(R.layout.tt_activity_forgot_password);
        // 绑定布局资源(注意放所有资源初始化之前)
        LayoutInflater.from(this).inflate(R.layout.tt_activity_forgot_password, topContentView);
        initView();
        uiControlsOperation();

    }

    private void initView() {

        mLoginStatusView =(LinearLayout)findViewById(R.id.forgot_password_status);

        mEditTextForgotPasswordName =(EditText)findViewById(R.id.forgot_password_name);
        mPhoneVerificationCode =(PhoneVerificationCode)findViewById(R.id.phoneverification_pwd);
        mEditTextForgotPasswordCode= (EditText)findViewById(R.id.forgot_password_code);//输入验证码

        mEditTextNewPassword =(EditText)findViewById(R.id.new_password);
        mEditTextConfirmPassword =(EditText)findViewById(R.id.confirm_password);
        mNewPasswordButton =(Button)findViewById(R.id.new_password_in_button);//完成

    }
    private void uiControlsOperation() {

        inputOperation(mEditTextForgotPasswordName, findViewById(R.id.forgot_password_name_line));
        inputOperation(mEditTextForgotPasswordCode, findViewById(R.id.forgot_password_code_line));
        inputOperation(mEditTextNewPassword, findViewById(R.id.new_password_line));
        inputOperation(mEditTextConfirmPassword, findViewById(R.id.confirm_password_line));

        mPhoneVerificationCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditTextForgotPasswordName.getText().toString().trim().length() > 0) {
                    IMForgotPasswordManager.instance().checkWhetherUserExist(mEditTextForgotPasswordName.getText().toString().trim(), ForgotPasswordActivity.this, mPhoneVerificationCode);//验证用户是否存在，得到验证码
                } else {
                    Toast.makeText(ForgotPasswordActivity.this, "请输入正确的手机号码", Toast.LENGTH_LONG).show();
                }
            }
        });


        mNewPasswordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditTextForgotPasswordName.getText().toString().trim().length() > 0) {
                    if (mEditTextForgotPasswordCode.getText().toString().trim().length() > 0) {
                        if (mEditTextNewPassword.getText().toString().trim().length() > 0 || mEditTextConfirmPassword.getText().toString().trim().length() > 0) {
                            IMForgotPasswordManager.instance().passwordsConsistent(mEditTextNewPassword.getText().toString().trim(), mEditTextConfirmPassword.getText().toString().trim());//验证两次密码是否一致

                        } else {
                            Toast.makeText(ForgotPasswordActivity.this, "请输入正确的密码", Toast.LENGTH_LONG).show();
                        }

                    } else {
                        Toast.makeText(ForgotPasswordActivity.this, "请输入正确的验证码", Toast.LENGTH_LONG).show();
                    }

                } else {
                    Toast.makeText(ForgotPasswordActivity.this, "请输入正确的手机号码", Toast.LENGTH_LONG).show();
                }
            }
        });

        showTopInformation();

    }


    /**
     * 显示顶部信息
     */
    private void showTopInformation(){
        setTopBarDisplay();

        setCenterText("忘记密码");
    }

    /**
     * 输入焦点是否失去
     * @param mEditText
     * @param v
     */
    private void inputOperation(EditText mEditText,final View v){
        mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View arg0, boolean arg1) {
                // TODO Auto-generated method stub
                //如果失去焦点
                if (arg1 == false) {
                   v.setBackgroundColor(Color.rgb(211,211,211));//灰色
                }
                //如果获得焦点
                else {
                   v.setBackgroundColor(Color.rgb(3,192,43));//青色
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//卸载注册事件
    }

    /**
     * ----------------------------event 事件驱动----------------------------
     */
    public void onEventMainThread(ForgotPasswordEvent.UserName event) {
        switch (event) {
            case USER_NAME_EXIST:

                break;
            case USER_NAME_NO_EXIST:

                break;
        }
    }

    public void onEventMainThread(ForgotPasswordEvent.UserNewPassWord event) {
        switch (event) {
            case USER_NEW_PASSWORD_SAME:
                SubmitNewPassword();
                break;
            case USER_NEW_PASSWORD_NO_SAME:
                Toast.makeText(ForgotPasswordActivity.this, "两次输入密码不一致", Toast.LENGTH_LONG).show();
                break;
        }
    }

    /**
     * 是否退出
     * @param event
     */
    public void onEventMainThread(ForgotPasswordEvent.AnswerQuestion event) {
        switch (event) {
            case ANSWER_QUESTION_SAME:
                ForgotPasswordActivity.this.finish();//退出

                break;
            case ANSWER_QUESTION_NO_SAME:

                break;
        }
    }

    /**
     * 退出页面
     * @param view
     */
    public void onClickExit(View view){
        switch (view.getId()){
            case R.id.left_out_button:ForgotPasswordActivity.this.finish();break;
            default:break;
        }
    }


    /**
     * 是否显示进度页面
     * @param show
     */
    private void showProgress(final boolean show) {
        if (show) {
            mLoginStatusView.setVisibility(View.VISIBLE);
        } else {
            mLoginStatusView.setVisibility(View.GONE);
        }
    }

    /**
     * 提交新密码
     */
    private void SubmitNewPassword(){
        ForgotPasswordConstant.user_name = mEditTextForgotPasswordName.getText().toString().trim();
        ForgotPasswordConstant.user_new_password =mEditTextNewPassword.getText().toString().trim();
        ForgotPasswordConstant.user_code = mEditTextForgotPasswordCode.getText().toString().trim();
        IMForgotPasswordManager.instance().SubmitUserNewPassword(ForgotPasswordActivity.this);
    }
}
